import express from "express";
import fileController from "../controller/file.controller.js";
import tokenHandler from "../middleware/token.middleware.js";
import validateHandler from "../middleware/validate.middleware.js";
import { param, body, query } from "express-validator";
const router = express.Router();

//新建文件夹
router.post(
  "/folder/:parentId?",
  tokenHandler,
  [
    param("parentId")
      .optional()
      .isUUID()
      .withMessage("parentId must be an UUID"),
    body("filename").isString().withMessage("filename must be a String"),
  ],
  validateHandler,
  fileController.createDirectory,
);

//批量移动到回收站
router.patch(
  "/recycle/batch",
  tokenHandler,
  [body("fileIds").isArray().withMessage("ids must be an Array")],
  validateHandler,
  fileController.recycleFiles,
);

//移动到回收站
router.patch(
  "/recycle/:id",
  tokenHandler,
  [param("id").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.recycleFile,
);

//批量移动
router.patch(
  "/move/batch",
  tokenHandler,
  [
    body("fileIds").isArray().withMessage("ids must be an Array"),
    body("parentId")
      .optional({ nullable: true })
      .isUUID()
      .withMessage("parentId must be an UUID"),
  ],
  validateHandler,
  fileController.moveFiles,
);

//移动
router.patch(
  "/move/:id",
  tokenHandler,
  [
    param("id").isUUID().withMessage("id must be an UUID"),
    body("parentId")
      .optional({ nullable: true })
      .isUUID()
      .withMessage("parentId must be an UUID"),
  ],
  validateHandler,
  fileController.moveFile,
);
//复制
router.post(
  "/copy/:id",
  tokenHandler,
  [
    param("id").isUUID().withMessage("id must be an UUID"),
    body("parentId")
      .optional({ nullable: true })
      .isUUID()
      .withMessage("parentId must be an UUID")
  ],
  validateHandler,
  fileController.copyFile,
)

//批量删除
router.delete(
  "/batch",
  tokenHandler,
  [body("fileIds").isArray().withMessage("ids must be an Array")],
  validateHandler,
  fileController.deleteFiles,
);

//删除文件
router.delete(
  "/:id",
  tokenHandler,
  [param("id").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.deleteFile,
);

//批量恢复
router.patch(
  "/restore/batch",
  tokenHandler,
  [body("fileIds").isArray().withMessage("ids must be an Array")],
  validateHandler,
  fileController.restoreFiles,
);

//恢复文件
router.patch(
  "/restore/:id",
  tokenHandler,
  [param("id").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.restoreFile,
);

//重命名
router.patch(
  "/filename/:id",
  tokenHandler,
  [
    param("id").isUUID().withMessage("id must be an UUID"),
    body("filename").isString().withMessage("filename must be a String"),
  ],
  validateHandler,
  fileController.rename,
);

//使用直链下载
router.get(
  "/download/url/:signature",
  [param("signature").isString().withMessage("id must be an String")],
  validateHandler,
  fileController.downloadFileByUrl,
)

//下载文件夹
router.get(
  "/download/folder/:folderId",
  tokenHandler,
  [param("folderId").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.downloadFolder,
)

//批量下载文件
router.post(
  "/download/batch",
  tokenHandler,
  [body("Ids").isArray().withMessage("Ids must be an Array")],
  validateHandler,
  fileController.downloadFiles,
)


//使用直链播放视频
router.get(
  "/play/url/:signature",
  [param("signature").isString().withMessage("id must be an String")],
  validateHandler,
  fileController.palyVideoByUrl,
)
//下载文件
router.get(
  "/download/:id",
  tokenHandler,
  [param("id").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.downloadFile,
);

//获取文件直链签名
router.get(
  "/url/:fileId",
  tokenHandler,
  [param("fileId").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.getFileUrl,
)

// 获取目录列表
router.get(
  "/page/:parentId?",
  tokenHandler,
  [
    param("parentId").optional().isUUID().withMessage("id must be an UUID"),
    query("current").optional().isInt().withMessage("current must be an INT"),
    query("pageSize").optional().isInt().withMessage("pageSize must be an INT"),
    query("searchText")
      .optional()
      .isString()
      .withMessage("searchText must be a String"),
    query("sortField")
      .optional()
      .isString()
      .withMessage("sortField must be a String"),
    query("sortOrder")
      .optional()
      .isString()
      .withMessage("sortOrder must be a String"),
  ],
  validateHandler,
  fileController.getPage,
);

//查看回收站
router.get(
  "/recycle",
  tokenHandler,
  fileController.getRecycle
);

//获取文件夹
router.get(
  "/folders/:parentId?",
  tokenHandler,
  [
    param("parentId")
      .optional()
      .isUUID()
      .withMessage("parentId must be an UUID"),
  ],
  validateHandler,
  fileController.getFolders,
);

//获取文件信息
router.get(
  "/:id",
  tokenHandler,
  [param("id").isUUID().withMessage("id must be an UUID")],
  validateHandler,
  fileController.getFileInfo,
);

export default router;
